package com.cloud.sysmana.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.sysmana.entity.SysRole;
import com.cloud.sysmana.ser.ISysFuncService;
import com.cloud.sysmana.ser.ISysRoleService;
import com.cloud.util.base.BaseAction;
import com.cloud.util.base.RetType;
import com.cloud.util.page.PageInfo;

/**
 * 角色管理
 * @author zqq
 * @date 2016年4月26日
 */
@Controller
@RequestMapping("system")
public class RoleAction extends BaseAction{

	@Resource(name="sysRoleService")
	private ISysRoleService sysRoleService;
	
	@Resource(name="sysFuncService")
	private ISysFuncService sysFuncService;
	
	/**
	 * 角色列表
	 * @return
	 */
	@RequestMapping("roleList.do")
	public String roleList(ModelMap modelMap, PageInfo pageInfo){
		
		PageInfo retPageInfo = sysRoleService.listRolePage(pageInfo);
		modelMap.put("pageInfo", retPageInfo);
		
		return getViewsModelPathSys("sysmana/role/roleList");
	}
	
	/**
	 * 添加角色登录
	 * @return
	 */
	@RequestMapping("addRoleLogin.do")
	public String addRoleLogin(ModelMap modelMap, SysRole role){
		return getViewsModelPathSys("sysmana/role/addRole");
	}
	
	
	/**
	 * 添加或更新角色
	 * @param modelMap
	 * @param role
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addRole.ido")
	public RetType addOrUpdateFunc(ModelMap modelMap, SysRole role, HttpServletRequest request){
		return sysRoleService.addOrUpdateRole(role);
	}
	
	/**
	 * 编辑角色登录
	 * @return
	 */
	@RequestMapping("editRoleLogin.do")
	public String editRoleLogin(ModelMap modelMap, SysRole role){
		
		if(role != null && role.getRoleId() != null){
			role = sysRoleService.getRoleById(role.getRoleId());
		}
		modelMap.put("role", role);
		
		return getViewsModelPathSys("sysmana/role/editRole");
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delRole.ido")
	public RetType delRole(ModelMap modelMap, String ids){
		
		RetType reType = sysRoleService.delRole(ids);
		
		return reType;
	}
	
	/**
	 * 角色授权登录
	 * @return
	 */
	@RequestMapping("roleAuthLogin.do")
	public String roleAuthLogin(ModelMap modelMap, SysRole role){
		
		String roleTreeJson = sysFuncService.listRoleTreeJson(role);
		modelMap.put("roleId", role.getRoleId());
		modelMap.put("roleTree", roleTreeJson);
		
		return getViewsModelPathSys("sysmana/role/roleAuth");
	}
	
	/**
	 * 角色授权
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editRoleFunc.ido")
	public RetType editRoleFunc(ModelMap modelMap, String ids, int roleId){
		
		RetType reType = sysRoleService.editRoleFunc(ids, roleId);
		
		return reType;
	}
	
}
